<?php
	include_once "../config.inc.php";
	
	$eve = empty($_POST['eve']) ? 0 : $_POST['eve'];
	//进行返回提示问题
	if($eve == 1){
		//核对好用户名相同才进行增加购物车
		if($uid > 0){
			//加购物车分几步走 1.查询购物车有有无此商品,有则修改数量,无则增加
			//判断商品是否存在
			$stmt1 = $pdo->prepare('select gid,zkprice from kp_goods where gid=?');
			$stmt1->execute(array($_POST['gid']));
			$goodArr = $stmt1->fetch(PDO::FETCH_ASSOC);
			if(empty($goodArr)){
				echo '防止注入:商品不存在的错误输出';
				return;
			}
			//判断购物车中有无此商品,有则修改,无则增加
			$stmt = $pdo->prepare('select gid from kp_carlist where uid=? and gid=?');
			$stmt->execute(array($_POST['uid'],$_POST['gid']));
			$flag = false;
			//echo $stmt->rowCount();
			if($stmt->rowCount() > 0){
				//echo 'update kp_carlist set num=num+'.$_POST['num'].',newprice='.$goodArr['zkprice'].',newdate=NOW() where uid='.$_POST['uid'].' and gid='.$_POST['gid'];
				//在修改时改变初加购物车的价,后来注释掉了
				//$stmt = $pdo->prepare('update kp_carlist set num=num+?,newprice=?,newdate=NOW() where uid=? and gid=?');
				//$flag = $stmt->execute(array($_POST['num'],$goodArr['zkprice'],$_POST['uid'],$_POST['gid']));
				$stmt = $pdo->prepare('update kp_carlist set num=num+?,newdate=NOW() where uid=? and gid=?');
				$flag = $stmt->execute(array($_POST['num'],$_POST['uid'],$_POST['gid']));
			}else{
				$stmt = $pdo->prepare('insert into kp_carlist(uid,gid,num,newprice) values(?,?,?,?)');
				$flag = $stmt->execute(array($_POST['uid'],$_POST['gid'],$_POST['num'],$goodArr['zkprice']));
			}
			if($flag){
				echo '添加到购物车成功';
			}else{
				echo '添加到购物车成功';
			}
		}
		//2为修改购物车中商品
	}elseif($eve == 2){
		//赶时间,不封BUG了
		$flag = false;
		if($uid > 0){
			$stmt = $pdo->prepare('update kp_carlist set num=? where uid=? and gid=?');
			$flag = $stmt->execute(array($_POST['num'],$uid,$_POST['gid']));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}		
		
		//3为删除购物车中商品
	}elseif($eve == 3){
		$flag = false;
		if($uid > 0){
			$stmt = $pdo->prepare('delete from kp_carlist where uid=? and gid=?');
			$flag = $stmt->execute(array($uid,$_POST['gid']));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
		//4为结算:TMD下次写成存储过程.先就这么用着
	}elseif($eve == 4){
		$flag = false;
		$payid = '';
		if($uid > 0){
			//1.执行存储过程得到订单号
			$stmt = $pdo->prepare('CALL create_sid("SS",8,?,@?)');
			$stmt->execute(array($uid,0));
			$arr = $stmt->fetch(PDO::FETCH_ASSOC);
			//print_r($arr['newsid']);//newsid = SS2017072100025
			//2.操作订单数据:gids   2.1写入到订单明细表;2.2把订单表处理下;2.3删除购物车中商品;2.4在界面显示下详情.
			$payid = $arr['newsid'];
			
			//写入订单明细:gids
			$sql = 'insert into kp_sell select \''.$payid.'\',a.gid,a.num,b.price,b.zkprice,NULL,NULL,NULL FROM (select gid,num FROM KP_carlist where uid = ? and gid in('.$_POST['gids'].')) a LEFT JOIN kp_goods b ON a.gid = b.gid';
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array($uid));
			
			//处理订单.
			$sql = 'update kp_sell_order set overdate = NOW(),addressid=?,payid=?,ymoney=(select sum(num*price) from kp_sell where sid = \''.$payid.'\'),zkmoney=(select sum(num*zkprice) from kp_sell where sid = \''.$payid.'\'),zmoney=(select sum(num*zkprice) from kp_sell where sid = \''.$payid.'\'),zt=1 where sid = \''.$payid.'\'';
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array($_POST['address'],$_POST['pay']));
			
			//处理掉购物车中商品
			$sql = 'delete from kp_carlist where uid=? and gid in('.$_POST['gids'].')';
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array($uid));
			
			//减掉用户余额
			$sql = 'update kp_usr set money=money-(select sum(num*zkprice) from kp_sell where sid = \''.$payid.'\') where uid = ?';
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array($uid));
			
			//减掉库存数量
			$sql = 'update kp_goods a inner join kp_sell b on b.sid=\''.$payid.'\' and a.gid = b.gid set a.num=a.num-b.num';
			$stmt = $pdo->prepare($sql);
			$stmt->execute();
			echo '({"eve":"1","newsid":"'.$arr['newsid'].'"})';
		}
		//5修改购物车商品选中状态
	}elseif($eve == 5){
		$flag = false;
		if($uid > 0){
			//print_r($_POST['gid']);
			//判断传过来的状态是否选中
			$zt = $_POST['zt'] == 'true' ? '1' : '0';
			//echo 'zt:'.$zt;
			//判断传过来的商品ID
			if($_POST['gid'] > 0){
				$stmt = $pdo->prepare('update kp_carlist set checked=? where uid=? and  gid=?');
				$flag = $stmt->execute(array($zt,$uid,$_POST['gid']));
			}else{
				$stmt = $pdo->prepare('update KP_carlist set checked=? where uid=?');
				$flag = $stmt->execute(array($zt,$uid));	
			}
			if($flag){
				echo '1';
			}else{
				echo '0';
			}
		}
		//6是商品评价
	}elseif($eve == 6){
		$flag = false;
		if($uid > 0){
			$stmt = $pdo->prepare('update kp_sell set evaluate=?,evaldate=NOW() where sid=? and gid=?');
			$flag = $stmt->execute(array($_POST['centent'],$_POST['sid'],$_POST['gid']));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
	//修改用户信息状态
	}elseif($eve == 7){
		$flag = false;
		if($uid > 0){
			$stmt = $pdo->prepare('update kp_u_msg set zt=1,overdate=NOW() where touid=? and zt=0');
			$flag = $stmt->execute(array($uid));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
		//修改用户收货地址
	}elseif($eve == 8){
		$flag = false;
		if($uid > 0){
				/*eve: 8
			,id: id
			,uname: uname
			,uaddress: uaddress
			,utel: utel
			,ucode: ucode
			,udefault: udefault*/
			$isdefault = 0;
			if($_POST['udefault'] == 'true'){
				$isdefault = 1;
				$stmt = $pdo->prepare('update kp_usr_address set isdefault=0 where id <> ? and uid=?');
				$flag = $stmt-> execute(array($_POST['id'],$uid));
			}
			$stmt = $pdo->prepare('update kp_usr_address set uname=?,address=?,tel=?,postcode=?,isdefault=? where id=? and uid=?');
			$flag = $stmt-> execute(array($_POST['uname'],$_POST['uaddress'],$_POST['utel'],$_POST['ucode'],$isdefault,$_POST['id'],$uid));
			
			
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
		//9为删除收货地址  注意:如果传过来的是默认地址,则还需进行修改下
	}elseif($eve == 9){
		$flag = false;
		if($uid > 0){
			$stmt = $pdo->prepare('delete from kp_usr_address where id=? and uid=?');
			$flag = $stmt-> execute(array($_POST['id'],$uid));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
		//增加收货地址
	}elseif($eve == 10){
		$flag = false;
		if($uid > 0){
			$isdefault = 0;
			if($_POST['udefault'] == 'true'){
				$isdefault = 1;
				$stmt = $pdo->prepare('update kp_usr_address set isdefault=0 where  uid=?');
				$flag = $stmt-> execute(array($uid));
			}

			$stmt = $pdo->prepare('insert into kp_usr_address(uid,uname,address,tel,postcode,isdefault) values(?,?,?,?,?,?)');
			$flag = $stmt-> execute(array($uid,$_POST['uname'],$_POST['uaddress'],$_POST['utel'],$_POST['ucode'],$isdefault));
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
		//11为用户确认收货
	}elseif($eve == 11){
		$flag = false;
		if($uid > 0){
			$flag = FALSE;
			$stmt = $pdo->prepare('update kp_sell_order set zt=3 where sid=?');
			$flag = $stmt->execute([$_POST['sid']]);
			if($flag){
			echo '1';
		}else{
			echo '用户确认收货失败!';
		}
		}
	}
	
	
	/*
	elseif($eve == 9){
		$flag = false;
		if($uid > 0){
			
		}
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
	}
	 * */
?>